package com.example.Film.reviews.website.controllers;

import com.example.Film.reviews.website.dao.DirectorRepository;
import com.example.Film.reviews.website.dao.EvaluationRepository;
import com.example.Film.reviews.website.dao.MovieRepository;
import com.example.Film.reviews.website.dao.PerformerRepository;
import com.example.Film.reviews.website.entities.Director;
import com.example.Film.reviews.website.entities.Evaluation;
import com.example.Film.reviews.website.entities.Movie;
import com.example.Film.reviews.website.entities.Performer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("movies")
public class MovieController {

    @Autowired
    MovieRepository movieRepository;
//    @Autowired
//    EvaluationRepository evaluationRepository;

    @Autowired
    DirectorRepository directorRepository;
    @Autowired
    PerformerRepository performerRepository;

    @GetMapping
    public String displayMovie(Model model){
      List<Movie> movies =movieRepository.findAll();
      model.addAttribute("movies",movies);
       return "movie/list-movie";
    }

    @GetMapping("/new")
    public String displayMovieForm(Model model){
        Movie  aMovie = new Movie();
        model.addAttribute("movie",aMovie);
        List<Director> directors= directorRepository.findAll();

        model.addAttribute("allDirectors",directors);

        List<Performer> performers= performerRepository.findAll();
        model.addAttribute("allPerformers",performers);
        return "movie/new-movie";
    }

    @PostMapping("/save")
    public String createMovie(Model model,@RequestParam List<Long> directors,@RequestParam List<Long> performers,Movie movie){
        movieRepository.save(movie);

        return "redirect:/movies";
    }
}
